import scrollOpt from '../base/scrollOptimize'
import { WX_AD_CONFIG, Sdk_Platform, VIVO_AD_CONFIG, Ad_Type } from '../../frame/constants';
import { UI_Layer, UI_Listen } from '../../config/ui_def'
cc.Class({
    extends: scrollOpt,

    properties: {
        testPre: cc.Prefab,
        topNode: cc.Node,
        bannerNode: cc.Node,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {
        // this.initPage()
        // this.testCreat()
        // this.passListAni()
        // this.scheduleServer()
        // this.openListen()
    },

    // update(dt) {
    //     // console.log(this.localConvertWorldPointAR(this.content.children[0]))
    // },


    initPage() {
        if (!he.pool.getPool('passItemBig')) {
            he.pool.createPoolSync('passItemBig', 5, cc.Prefab, 'uimod/passItemBig')
        }
        if (!he.pool.getPool('passItem')) {
            he.pool.createPoolSync('passItem', 10, cc.Prefab, 'uimod/passItem')
        }
    },

    updatePage(param) {
        // this.scrollInit()
        this.adRes = null
        this.getAd(true)
        if (param.pass_mainId) {
            this.pass_mainId = param.pass_mainId
        } else {
            this.pass_mainId = 1
        }
        this.scroll.scrollToTop()
        this.aniGrp = []
        this.passItemIndex = 0
        gg.eventManager.on('roleInfo', this.listenClient, this)
        // this.creatPassList()
        this.xScroll_scroll = this.scroll.getComponent('Xscroll')
        this.xScroll_scroll.init([
            {
                pool: 'passItemBig',
                type: cc.Prefab,
                url: 'uimod/passItemBig',
                num: 5
            },
        ], this.scrollInfoInit.bind(this), {
            default: 'passItemBig',
        })
        this.passListAni()
        this.scheduleServer()
        // this.openListen()
        this.adSwitch(true)
        this.isShowBanner = false

    },

    scrollInfoInit() {
        let scrollList = []
        let passInfo = he.db.getTable('Pass')
        if (!passInfo) {
            console.log('pass表丢失')
            return
        }
        if (passInfo && passInfo.length > 0) {
            for (let i = (passInfo.length - 1); i > -1; i--) {
                if (this.pass_mainId == passInfo[i].main_pass) {
                    scrollList[i] = {}
                    scrollList[i].info = passInfo[i]
                }
            }
            if (this.adRes != null) {
                let adNum = this.randomNum(4, 9)
                scrollList.splice(adNum, 0, { info: { isAd: true, adInfo: this.adRes } })
            }
            scrollList.map(pass => {
                pass.name = 'passItemBig'
                pass.script = 'passItemBig'
                // 这里加一部星星的等级判定
                return pass
            })
            console.log(scrollList)
            this.xScroll_scroll.set_data(scrollList)
            this.xScroll_scroll.scroll_to_start()
        }
    },
    //范围内的随机数
    randomNum(minNum, maxNum) {
        switch (arguments.length) {
            case 1:
                return parseInt(Math.random() * minNum + 1, 10);
                break;
            case 2:
                return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
                break;
            default:
                return 0;
                break;
        }
    },

    getAd(param) {
        switch (common.sdk_env) {
            case Sdk_Platform.WECHAT:
                break
            case Sdk_Platform.VIVO:
                this.vivoAdInit()
                break
            case Sdk_Platform.OPPO:
                break
            case Sdk_Platform.UC:
                he.ad.showInterstitialAd()
                break
            default:
                // this.node.active = false
                break
        }
    },

    vivoAdInit() {
        let adInfo = he.ad.getNativeJson(VIVO_AD_CONFIG.nativeAd[4], this.successServer.bind(this), this.failServer.bind(this))
        if (!adInfo) {
            this.failServer()
        } else if (adInfo === true) {

        } else if (adInfo) {
            this.successServer(adInfo)
        }

    },

    successServer(res) {
        if (res == [] || res == null || res == undefined) {
            this.adRes = null
            return
        } else {
            this.adRes = res
        }

    },

    failServer() {
        this.adRes = null
    },




    closeUI() {
        this.clearAni()
        for (let i = (this.content.children.length); i > 0; i--) {
            // this.putToPool(this.content.children[i - 1])
            // this.content.children[i].opacity = 255;
            this.content.children[i - 1].getComponent('passItemBig').clearItem()
            he.pool.putElement('passItemBig', this.content.children[i - 1])
        }
        for (let j = (this.topNode.children.length); j > 0; j--) {
            this.topNode.children[j - 1].getComponent('scrollAd').clearItem()
            he.pool.putElement('scrollAd', this.topNode.children[j - 1])
        }
        this.stopSchedule()
        this.scroll.scrollToTop()
        this.node.targetOff(this)
        this.closeListen()
        if (this.sc1) {
            this.unschedule(this.bannerInit, 0.5)
            this.sc1 = null
        }
        if (this.sc2) {
            this.unschedule(this.hideBanner, 2.5)
            this.sc2 = null
        }
        this.adSwitch(false)
    },
    adSwitch(status) {
        if (status) {
            switch (common.sdk_env) {
                case Sdk_Platform.WECHAT:
                    if (!common.showAd) return
                    break
                // case 
                case Sdk_Platform.OPPO:
                    let guideList = he.local.getJson('guideList', 'user')
                    if (!guideList[6]) {
                        return
                    }
                    gg.uiManager.addPage(UI_Layer.Msg, {
                        name: 'chayeAdFakeOPPO',
                        isShowAd: true,
                        isShowBanner: false,
                        adIndex: 8,
                        position: cc.Vec2(0, -650)
                    })
                    break
                case Sdk_Platform.HUAWEI:
                    // let showPit = false
                    gg.uiManager.addPage(UI_Layer.Msg, {
                        name: 'hwAdPage',
                        cover: false,
                        isShowAd: true,
                        adType: Ad_Type.INSERT,
                        position: 0
                    })
                    break
            }
        } else {
            //  关掉广告
            switch (common.sdk_env) {
                case Sdk_Platform.OPPO:
                    gg.uiManager.closePage(UI_Layer.Msg, 'chayeAdFakeOPPO')
                    break
                case Sdk_Platform.HUAWEI:
                    if (gg.uiManager.getPage(UI_Layer.Msg, 'hwAdPage')) {
                        gg.uiManager.closePage(UI_Layer.Msg, 'hwAdPage')
                    }
                    break
            }
        }
    },


    colseServer() {
        switch (common.sdk_env) {
            case Sdk_Platform.WECHAT:
                if (!this.isShowBanner) {
                    this.isShowBanner = true
                    switch (common.sdk_env) {
                        case Sdk_Platform.WECHAT:
                            let ver_info = he.local.getJson('ver_info')
                            if (ver_info.banner_mistake) {
                                this.sc1 = this.scheduleOnce(this.bannerInit, 1)
                                this.sc2 = this.scheduleOnce(this.hideBanner, 3)
                            } else {
                                this.uiBase.closePage()
                            }
                            break
                    }
                } else {
                    this.uiBase.closePage()
                }
                break
            default:
                this.uiBase.closePage()
                break
        }

    },

    realClose() {
        this.uiBase.closePage()
    },
    bannerInit() {
        this.bannerNode.name = WX_AD_CONFIG.bannerId[0]
        /// 显示
        switch (common.sdk_env) {
            case Sdk_Platform.WECHAT:
                he.ad.changeBanner(true, this.bannerNode)
                break
        }
    },

    hideBanner() {
        switch (common.sdk_env) {
            case Sdk_Platform.WECHAT:
                he.ad.changeBanner(false, this.bannerNode)
                break
        }
    },
    topSet(config) {
        if (config.isOpen) {
            let pre = he.pool.getElement('scrollAd')
            pre.parent = this.topNode
            pre.getComponent('scrollAd').init(config.creatives, 1, true)
        }
    },

    //回收passItemBig的passItem
    putToPool(node) {
        let content = node.getChildByName('itemScroll').getChildByName('view').getChildByName('content')
        for (let i = (content.children.length); i > 0; i--) {
            he.pool.putElement('passItem', content.children[i - 1])
        }
    },


    listenClient(code, param, arg) {
        if (code.type) {
            switch (code.type) {
                case 0:
                    this.choosePass(code)
                    break

            }
        }
    },
    //选择了关卡
    choosePass(info) {

    },

    clearAni() {
        if (this.aniGrp && this.aniGrp.length != 0) {
            for (let i = 0; i < this.aniGrp.length; i++) {
                this.aniGrp[i].stop()
            }
        }
        this.aniGrp = []
    },

    testCreat() {
        for (let i = 0; i < 20; i++) {
            let pre = cc.instantiate(this.testPre)
            pre.parent = this.content
            pre.getComponent('passItemBig').clearItem()
            pre.getComponent('passItemBig').initPassInfo(i)
        }
        this.scroll.scrollToTop()

    },

    creatPassList() {
        let passInfo = he.db.getTable('Pass')
        if (!passInfo) {
            console.log('pass表丢失')
            return
        }
        if (passInfo && passInfo.length > 0) {
            for (let i = 0; i < passInfo.length; i++) {
                let pre = he.pool.getElement('passItemBig')
                pre.opacity = 255
                pre.parent = this.content
                // console.log(passInfo[i])
                pre.getComponent('passItemBig').initInfo(passInfo[i])///////////////////////////应该是   passInfo【i】
            }
        }
        this.scroll.scrollToTop()
    },

    passListAni() {
        let passContentLayout = this.content.getComponent(cc.Layout)
        // passContentLayout.enabled = false
        this.content.children.forEach(element => {
            element.x += 800
        });
        this.isAni = true
    },

    passListInit() {
        if (this.isAni) {
            this.content.children.forEach(element => {
                element.x = 0
            });
            this.isAni = false
        }

    },

    scheduleServer() {
        this.schedule(this.aniServer, 0.1, this.content.children.length, 0)
    },

    stopSchedule() {
        this.unschedule(this.aniServer)
    },

    aniServer() {
        if (!this.content.children) return
        if (this.passItemIndex >= this.content.children.length || !this.isAni) {
            this.stopSchedule()
            return
        }
        this.aniGrp[this.passItemIndex] = cc.tween(this.content.children[this.passItemIndex])
            .to(0.3, { x: 0 })
            .start()
        this.passItemIndex += 1
    },

});
